<?php
/**
* @package   Anton Getman - ordasoft.com
* @copyright  Copyright (C) 2011 andrey Kvasnevskiy - ordasoft.com. All rights reserved
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');
if( !function_exists( 'sefreltoabs')) {
  function sefRelToAbs( $value ) {
    //Need check!!!

    // Replace all &amp; with & as the router doesn't understand &amp;
    $url = str_replace('&amp;', '&', $value);
    if(substr(strtolower($url),0,9) != "index.php") return $url;
    $uri    = JURI::getInstance();
    $prefix = $uri->toString(array('scheme', 'host', 'port'));
    return $prefix.JRoute::_($url);
  }
}


class modSimilaireHelper
{
  function getList($params,$nbrAffiche)
  {
    global $mainframe;

    $db         =& JFactory::getDBO();
    $user =& JFactory::getUser();

    $option       = JRequest::getCmd('option');
    $func       = JRequest::getCmd('func');
    $id         = JRequest::getString('id');
    $itemid       = JRequest::getString('Itemid');

    $aid = $user->get('aid', 0);

    //$showDate     = $params->get('showDate', 0);
    $conf =& JFactory::getConfig();
    //echo $option;
      //return true ; //$related = array();
      /*
    if ($option == 'com_vehiclemanager' && $func == 'detail' && $id){
        $related = modSimilaireHelper::getSimilaire($id,$nbrAffiche);
    } else {
      return true ; //$related = array();
    }*/
        $related = modSimilaireHelper::getSimilaire($id,$nbrAffiche,$params);

    return $related;
  }

  function getSimilaire($id,$nbrAffiche,$params) {
    $db =& JFactory::getDBO();
    $user =& JFactory::getUser();
    $date =& JFactory::getDate();
    //return true;
    $related = array();

    $nullDate = $db->getNullDate();
    $now  = $date->toMySQL();


  
    // select other items based on the metakey field 'like' the keys found
    $query = 'SELECT c.id AS cid, a.id, a.htitle, a.year, a.bedrooms, a.hcity, a.image_link, a.price, a.catid' .
        ' FROM #__rem_houses AS a, #__rem_main_categories AS c , #__rem_categories AS vc ' .
        ' WHERE a.id <> '.(int) $id . " and c.section='com_realestatemanager' ".
              " AND c.published='1' ".
              " AND vc.iditem=a.id ".
              " AND vc.idcat = c.id ".
              " AND a.published='1' ".
              " AND a.approved='1' " ;
        
        if($params->get('optBedrooms')==0)    $query .= ' and a.bedrooms =(select bedrooms from #__rem_houses t where t.id = '.(int) $id . ')';
        if($params->get('optCategory')==0) $query .= ' and vc.idcat =(select idcat from #__rem_categories t2 where t2.iditem = '.(int) $id . ')';
        if($params->get('optCity')==0) $query .= ' and a.hcity =(select hcity from #__rem_houses t3 where t3.id = '.(int) $id . ')';
              
         $query .=  " GROUP BY a.id ".
              " ORDER BY a.date desc" .
              ' limit ' . $nbrAffiche ;
    $db->setQuery($query);
    $temp = $db->loadObjectList();
    echo "<!--";
    print_r($temp);
    print_r($db);
    echo "-->";
    /*if (count($temp)){
      foreach ($temp as $row){
        if (($row->cat_state == 1 || $row->cat_state == '') && ($row->sec_state == 1 || $row->sec_state == '') && ($row->cat_access <= $user->get('aid', 0) || $row->cat_access == '') && ($row->sec_access <= $user->get('aid', 0) || $row->sec_access == '')) {
          $row->route = JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug, $row->sectionid));
          $related[] = $row;
        }
      }
    }*/
    //  echo '<br />nb=' .count($temp);
    //unset ($temp);
      
    
    return $temp;
  } 
}
?>
